package com.work.biz.controller;

import java.util.List;

import com.work.mp.service.impl.MpTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.work.common.annotation.Log;
import com.work.common.core.controller.BaseController;
import com.work.common.core.domain.AjaxResult;
import com.work.common.enums.BusinessType;
import com.work.biz.domain.BizStudent;
import com.work.biz.service.IBizStudentService;

/**
 * 学生Controller
 *
 * @author lee
 * @date 2021-04-22
 */
@RestController
@RequestMapping("/api/biz/student")
@Api(value = "学生api", tags = {"查询班级所有学生,学生加入班级接口"})
public class BizStudentController extends BaseController {
    @Autowired
    private IBizStudentService bizStudentService;
    @Autowired
    private MpTokenService tokenService;

    /**
     * 查询学生列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询班级所有学生", httpMethod = "POST", notes = "成功：0，验证失败：500")
    public AjaxResult list(
            @ApiParam(value = "班级数据", required = true)
            @RequestBody
                    BizStudent bizStudent
    ) {
        List<BizStudent> list = bizStudentService.selectBizStudentList(bizStudent);
        return AjaxResult.success(list);
    }

    /**
     * 获取学生详细信息
     */
    @GetMapping(value = "/{studentId}")
    public AjaxResult getInfo(@PathVariable("studentId") Long studentId) {
        return AjaxResult.success(bizStudentService.selectBizStudentById(studentId));
    }

    /**
     * 新增学生
     */
    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "学生加入班级", httpMethod = "POST", notes = "成功：0，验证失败：500")
    public AjaxResult add(
            @ApiParam(value = "用户token", required = true)
            @RequestHeader("tk")
                    String tk,
            @ApiParam(value = "学生数据", required = true)
            @RequestBody BizStudent bizStudent
    ) {
        bizStudent.setUserId(tokenService.getUserId(tk));
        return toAjax(bizStudentService.insertBizStudent(bizStudent));
    }

    /**
     * 修改学生
     */
    @Log(title = "学生", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    @ApiOperation(value = "学生退出或移除班级", httpMethod = "PUT", notes = "成功：0，验证失败：500")
    public AjaxResult edit(
            @ApiParam(value = "用户token", required = true)
            @RequestHeader("tk")
                    String tk,
            @RequestBody BizStudent bizStudent
    ) {
        bizStudent.setDelFlag("2");
        bizStudent.setUpdateBy(tokenService.getUserId(tk).toString());
        return toAjax(bizStudentService.updateBizStudent(bizStudent));
    }

    /**
     * 删除学生
     */
    @Log(title = "学生", businessType = BusinessType.DELETE)
    @DeleteMapping("/{studentId}")
    public AjaxResult remove(@PathVariable Long studentId) {
        return toAjax(bizStudentService.deleteBizStudentById(studentId));
    }
}
